<?php

require_once './application/core/CoreBase.php';
require_once './application/models/Tables/cache.php';

class clientSoap extends CoreBase
{

    private $_opt;
    private $_clnt;
    private $_service;
    private $_obj;
    private $_header;
    private $_body;
    private $_serviceNode;
    private $_nodeNode;
    private $_idClient;
    private $_modeleSoap;
    private $_key;
    private $_val;
    private $_path;

    public function __construct(&$controller)
    {
        parent::__construct($controller);
        $this->_modeleSoap = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" .
                "<soap:Envelope xmlns:soap=\"http://www.w3.org/2001/12/soap-envelope\" soap:encodingStyle=\"http://www.w3.org/2001/12/soap-encoding\">" .
                "</soap:Envelope>";
        $this->_path = $this->_controller->_urlBase . "/Server";
        $this->_idClient = $this->_controller->_idClient;
    }

    // Client SOAP
    public function client($uri, $location, $xml)
    {
        if (!isset($uri) || !isset($location) || !isset($xml))
            return "Error args";
        try
        {
            //set options
            $this->_opt = array('encoding' => 'utf-8',
                'trace' => true,
                'exceptions' => true,
                'uri' => $uri,
                'user_agent' => "ADN-Service",
                'location' => $location,
                'connection_timeout' => '28'
            );
            $this->_clnt = new SoapClient(null, $this->_opt);
            //do the request
            $this->_clnt->__doRequest($xml, $location, $uri, SOAP_1_2, true);
        } catch (Exception $e)
        {
            echo get_class($e);
            echo $e->getMessage();
        }
    }

    // Construct urn
    public function uriSoap($groupe, $service)
    {
        return "urn:" . $groupe . "/" . $service;
    }

    // Contruct Soap XML
    public function xmlSoap($uri, $serviceResp, $node, $params)
    {
        if (!isset($node))
            return "Error Node";
        if (!isset($uri))
            return "Error uri";
        if (!isset($serviceResp))
            return "Error ServiceResp";
        if (!is_array($params) && !isset($params))
            return "Error Params";
        //set variables
        $uri = substr($uri, 4);
        $this->_service = explode('/', $uri);
        //construct the xml
        $this->_obj = new SimpleXMLElement($this->_modeleSoap);
        $this->_header = $this->_obj->addChild('Header', null);
        $this->_header->addChild('IpServices', "http://" . $_SERVER['HTTP_HOST'] . $this->_path);
        $this->_header->addChild('IdClient', $this->_idClient);
        $this->_header->addChild('urn', $serviceResp . "/" . $uri);
        $this->_body = $this->_obj->addChild('Body', null);
        $this->_serviceNode = $this->_body->addChild($this->_service[1], null);
        $this->_nodeNode = $this->_serviceNode->addChild($node, null);
        foreach ($params as $this->_key => $this->_val)
            $this->_nodeNode->addChild($this->_key, $this->_val);
        return $this->_obj->asXML();
    }

}
